package cn.wendellup.bootStrap.valueobject.entity;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.jdbc.core.RowMapper;

import cn.egame.common.util.Utils;
import cn.wendellup.bootStrap.valueobject.constants.EnumType.AppParameterParamType;
import cn.wendellup.bootStrap.valueobject.constants.EnumType.AppParameterType;

public class AppParameter implements Serializable, RowMapper<AppParameter> {
	private static final long serialVersionUID = 1L;
	
	public static class FieldStatus {
		public static int parentId = 1 << 0;
		public static int type = 1 << 1;
		public static int name = 1 << 2;
		public static int param = 1 << 3;
		public static int sort = 1 << 4;
		public static int enable = 1 << 5;
		public static int depth = 1 << 6;
		public static int remark = 1 << 7;
		public static int picId = 1 << 8;
		public static int beginTime = 1 << 9;
		public static int endTime = 1 << 10;
		public static int updateTime = 1 << 11;
		public static int operatorId = 1 << 12;
	}
	
	private int updateStatus = 0;
	
	private int id = 0;
	private int parentId = 0;
	private AppParameterType type;
	private String name;
	private Map<AppParameterParamType, String> param = new HashMap<AppParameterParamType, String>();
	private int sort = 0;
	private boolean enable = true;
	private int depth = 0;
	private String remark = "";
	private long picId = 0;
	private Date beginTime = new Date();
	private Date endTime = new Date();
	private Date updateTime = new Date();
	private int operatorId = 0;

	public boolean Enable() {
		return enable;
	}

	public int getDepth() {
		return depth;
	}

	

	public int getId() {
		return id;
	}

	public int getParentId() {
		return parentId;
	}

	public Map<AppParameterParamType, String> getParam() {
		return param;
	}


	public String getRemark() {
		return remark;
	}

	public int getSort() {
		return sort;
	}

	public AppParameterType getType() {
		return type;
	}

	public boolean getValueBoolean(AppParameterParamType key) {
		return this.getValueInt(key, 0) > 0;
	}

	public int getValueInt(AppParameterParamType key, int defaultValue) {
		return Utils.toInt(this.param.get(key), 0);
	}

	public String getValueString(AppParameterParamType key) {
		return this.param.get(key);
	}

	public boolean isEnable() {
		return enable;
	}

	public void setDepth(int depth) {
		if (this.depth != depth) {
			this.depth = depth;
			this.updateStatus = this.updateStatus
					| AppParameter.FieldStatus.depth;
		}
	}

	public void setEnable(boolean enable) {
		if (this.enable != enable) {
			this.enable = enable;
			this.updateStatus = this.updateStatus
					| AppParameter.FieldStatus.enable;
		}
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setParentId(int parentId) {
		if (this.parentId != parentId) {
			this.parentId = parentId;
			this.updateStatus = this.updateStatus
					| AppParameter.FieldStatus.parentId;
		}
	}

	public void setRemark(String remark) {
		if (this.remark != remark) {
			this.remark = remark;
			this.updateStatus = this.updateStatus
					| AppParameter.FieldStatus.remark;
		}
	}

	public void setSort(int sort) {
		if (this.sort != sort) {
			this.sort = sort;
			this.updateStatus = this.updateStatus
					| AppParameter.FieldStatus.sort;
		}
	}

	public void setType(AppParameterType type) {
		if (this.type != type) {
			this.type = type;
			this.updateStatus = this.updateStatus
					| AppParameter.FieldStatus.type;
		}
	}

	public int getUpdateStatus() {
		return updateStatus;
	}

	public void setUpdateStatus(int updateStatus) {
		this.updateStatus = updateStatus;
	}

	public void setParam(Map<AppParameterParamType, String> param) {
		if (this.param != param) {
			this.param = param;
			this.updateStatus = this.updateStatus
					| AppParameter.FieldStatus.param;
		}
	}

	public long getPicId() {
		return picId;
	}

	public void setPicId(long picId) {
		if (this.picId != picId) {
			this.picId = picId;
			this.updateStatus = this.updateStatus
					| AppParameter.FieldStatus.picId;
		}
	}

	public Date getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(Date beginTime) {
		if (this.beginTime != beginTime) {
			this.beginTime = beginTime;
			this.updateStatus = this.updateStatus
					| AppParameter.FieldStatus.beginTime;
		}
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		if (this.endTime != endTime) {
			this.endTime = endTime;
			this.updateStatus = this.updateStatus
					| AppParameter.FieldStatus.endTime;
		}
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		if (this.updateTime != updateTime) {
			this.updateTime = updateTime;
			this.updateStatus = this.updateStatus
					| AppParameter.FieldStatus.updateTime;
		}
	}

	public int getOperatorId() {
		return operatorId;
	}

	public void setOperatorId(int operatorId) {
		if (this.operatorId != operatorId) {
			this.operatorId = operatorId;
			this.updateStatus = this.updateStatus
					| AppParameter.FieldStatus.operatorId;
		}
	}

	@Override
	public boolean equals(Object obj) {
		 if(obj != null && obj.getClass().equals(this.getClass())){
			 return this.getId() == ((AppParameter)obj).getId();
		 }
		 return false;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		if (this.name != name) {
			this.name = name;
			this.updateStatus = this.updateStatus
					| AppParameter.FieldStatus.name;
		}
	}

	@Override
	public AppParameter mapRow(ResultSet rs, int arg1) throws SQLException {
		AppParameter appParameter = new AppParameter();
		appParameter.setId(rs.getInt("id"));
		appParameter.setParentId(rs.getInt("parent_id"));
		appParameter.setType(AppParameterType.lookup(rs.getInt("type")));
		appParameter.setName(rs.getString("name"));
		
		String str = rs.getString("param");
        if (!Utils.stringIsNullOrEmpty(str)) {
            String[] kvs = str.split(",");
            if (kvs != null) {
                for (String kv : kvs) {
                    String[] k_v = kv.split("=");
                    if (k_v != null && k_v.length == 2) {
                        AppParameterParamType k = AppParameterParamType.lookup(k_v[0]);
                        if (k != AppParameterParamType.none)
                        	appParameter.getParam().put(k, k_v[1]);
                    }
                }
            }
        }
        appParameter.setSort(rs.getInt("sort"));
        appParameter.setEnable(rs.getBoolean("enable"));
        appParameter.setDepth(rs.getInt("depth"));
        appParameter.setRemark(rs.getString("remark"));
        appParameter.setPicId(rs.getLong("pic_id"));
        appParameter.setBeginTime(rs.getDate("begin_time"));
        appParameter.setEndTime(rs.getDate("end_time"));
        appParameter.setUpdateTime(rs.getDate("update_time"));
        appParameter.setOperatorId(rs.getInt("operator_id"));
		return appParameter;
	}

}
